#!/usr/bin/env bash

DEBUG=""

while getopts dh OPTION
do
    case ${OPTION} in
        d) DEBUG=1;;
	h) echo "Usage: regrtest.sh [ -d ] [ -h ]"
	    echo " -d    activates output of debug information"
	    echo " -h    prints this help message"
            exit 0;;
	\?) echo "Usage: regrtest.sh [ -d ] [ -h ]"
            exit 1;;
    esac
done

case "$(uname -s)" in
  Linux)
    BASEPATH=$(readlink -nf "$(dirname "$0")/..")
    ;;
  *)
    BASEPATH="$(dirname "$0")/.."
    ;;
esac

BUILDPATH="$BASEPATH/bin:$BASEPATH/lib/guava-10-1.0.1.jar"
EXAMPLEPATH="$BASEPATH/examples"
TESTPATH="$BASEPATH/test/files"

MAIN_CLASS="org.sosy_lab.ccvisu.CCVisu"
TEMPFILE="tmp.log"

PROGRAM="java -ea -classpath $BUILDPATH $MAIN_CLASS"
FAILTEXT="\033[31;1mThe test failed.\033[0m"
PASSTEXT="\033[32mThe test passed.\033[0m"


inittest() {
  echo "Testing: $1"
  if [ ! -z "$DEBUG" ] ; then
    echo "Command: $2"
  fi
}

regtest() {
  eval "$1"
  if [ $? -eq 0 ] ; then
    echo -e "  $PASSTEXT"
  else
    echo -e "  $FAILTEXT" >&2
  fi
  echo ""
}


TEST="-assertCheck"
inittest "Check if assertions are enabled." "$PROGRAM $TEST"
regtest "$PROGRAM $TEST >> /dev/null 2>&1"


TEST="-rsf-rsf-conversion-simple"
inittest "RSF → RSF conversion" "$PROGRAM $TEST"
regtest "[ $($PROGRAM $TEST | wc -c) -eq 39676 ]"


TEST="-rsf-rsf-conversion"
inittest "RSF → RSF conversion" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST | grep -v "^#" > $TEMPFILE && diff $EXAMPLEPATH/rsf-example.rsf $TEMPFILE >> /dev/null 2>&1"


TEST="-dox-rsf-conversion -setprop output.name=$TEMPFILE"
inittest "DOX → RSF conversion" "$PROGRAM $TEST"
regtest "[ $($PROGRAM $TEST && diff $EXAMPLEPATH/crocopat-2.1.4.rsf $TEMPFILE | wc -l) -eq 4 ]"


TEST="-ods-rsf-conversion -setprop output.name=$TEMPFILE"
inittest "ODS → RSF conversion" "$PROGRAM $TEST"
regtest "[ $($PROGRAM $TEST && diff $EXAMPLEPATH/crocopat-2.1.rsf $TEMPFILE | wc -l) -eq 4 ]"


TEST="-cvs-rsf-conversion -setprop output.name=$TEMPFILE"
inittest "CVS → RSF conversion" "$PROGRAM $TEST"
regtest "[ $($PROGRAM $TEST && diff $EXAMPLEPATH/crocopat-2.1.rsf $TEMPFILE | wc -l) -eq 4 ]"

IGNOREDATE="-I \".*20[1-9][0-9]-[01][0-9]-[0-3][0-9].*\""

TEST="-rsf-lay-conversion -setprop output.name=$TEMPFILE"
inittest "RSF → LAY conversion" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff $IGNOREDATE $EXAMPLEPATH/crocopat-2.1_annot.lay $TEMPFILE >> /dev/null 2>&1"


TEST="-lay-svg-conversion -setprop output.name=$TEMPFILE"
inittest "LAY → SVG conversion" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff -I \"<title>.*</title>\" -I \"20[1-9][0-9]-[01][0-9].*-->\" $EXAMPLEPATH/crocopat-2.1.svg $TEMPFILE >> /dev/null 2>&1"


TEST="-filter-sort -setprop output.name=$TEMPFILE"
inittest "Filter sort" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff $IGNOREDATE $TESTPATH/refClass_sort.rsf $TEMPFILE >> /dev/null 2>&1"


TEST="-filter-uniq -setprop output.name=$TEMPFILE"
inittest "Filter uniq" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff $IGNOREDATE $TESTPATH/refClass_uniq.rsf $TEMPFILE >> /dev/null 2>&1"


TEST="-filter-internal -setprop output.name=$TEMPFILE"
inittest "Filter internal" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff $IGNOREDATE $TESTPATH/refClass_internal.rsf $TEMPFILE >> /dev/null 2>&1"


TEST="-minimizer-determinicity -setprop output.name=$TEMPFILE"
inittest "Minimizer determinicity: 100 iterations of the minimizer (this may take a while)" "$PROGRAM $TEST"
regtest "$PROGRAM $TEST && diff $IGNOREDATE $EXAMPLEPATH/blast-1.1_random_100it.lay $TEMPFILE >> /dev/null 2>&1"


if [ -e "$TEMPFILE" ]; then
  rm "$TEMPFILE"
fi
